<template>
  <div>
    <a-row>
      <a-col>
        <a-button @click="handleTableSelect" type="primary">添加</a-button>
      </a-col>
      <br />
    </a-row>
    <a-table
      :columns="columns"
      :data-source="tableData"
      :loading="loading"
      :pagination="pagination"
      @change="handleTableChange"
      bordered
      :rowKey="(record, index) => record.id"
      :row-selection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
      }"
    >
    </a-table>
    <TableSelectModal
      :columns="goodModel.columns"
      :replaceFields="goodModel.replaceFields"
      :treeKey="goodModel.treeKey"
      :tableKey="goodModel.tableKey"
      :searchKey="goodModel.searchKey"
      :name="goodModel.name"
      :code="goodModel.code"
      :filename="goodModel.filename"
      :mode="goodModel.mode"
      ref="tableModal"
      @tableSelect="tableSelect"
    ></TableSelectModal>
  </div>
</template>

<script>
import TableSelectModal from "@/components/TableSelectModal/index.vue";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import {
  CommodityColumns,
  goodColumns,
} from "@/pages/OrderMent/components/tableConfig";
export default {
  mixins: [tableMinxins],
  components: { TableSelectModal },
  data() {
    return {
      columns: CommodityColumns,
      // 商品选中组件参数
      goodModel: {
        replaceFields: {
          children: "goodsTypeList",
          title: "goodsTypeName",
          key: "id",
          value: "id",
        },
        treeKey: "CommodityTypeFindAll",
        tableKey: "CommodityList",
        searchKey: "goodsType",
        name: "goodsName",
        code: "goodsCode",
        filename: "CommodityModal",
        columns: goodColumns,
        mode: "checkbox",
      },
      model: {},
    };
  },
  methods: {
    // 商品选择
    handleTableSelect() {
      this.$refs.tableModal.title = "商品";
      this.$refs.tableModal.modalAdd();
      if (this.selectedRowKeys.length > 0) {
        let newArr = this.selectedRowKeys.map((item) => {
          return Number(item);
        });
        this.$refs.tableModal.selectedRowKeys = newArr;
      } else {
        this.$refs.tableModal.selectedRowKeys = [];
      }
    },
    // 商品选择
    tableSelect(tableData, tableOption) {
      this.selectedRowKeys = [];
      this.tableData = tableData;
      tableData.forEach((item) => {
        this.selectedRowKeys.push(item.id);
      });
      this.$emit("ok", this.selectedRowKeys.toString());
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys;
      this.$emit("ok", this.selectedRowKeys.toString());
    },
  },
};
</script>
